﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblImageDAO
    {
        private DBService _db = null;
        public TblImageDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public DataTable TblImage_Ins(TblImage img, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("TblImage_InsUp",
                new string[] { "@IDImage", "@ItemID", "@ImageDetail", "@IDColor", "@ImageURL","@Mode" },
                new object[]{ img.IDImage, img.ItemID, img.ImageDetail, img.IDColor, img.ImageURL, mode }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetImage_ByItemID(int ItemID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public bool DeleteImgItem(TblImage img)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Del",
                    new string[] { "@p_ID" },
                    new object[] { img.IDImage },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public TblImage GetItemImageByID(int ItemID, int ID)
        {

            TblImage image = new TblImage();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblItemImage_Lst",
                   new string[] { "@p_ItemID", "@Id" },
                   new object[] { ItemID, ID },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    image.IDImage = Convert.ToInt32(dr["IDImage"].ToString());
                    image.ImageURL = dr["ImageURL"].ToString();
                    image.ItemID = Convert.ToInt32(dr["IDItem"].ToString());
                    image.ImageDetail = dr["ImageDetail"].ToString();
                    image.IDColor = Convert.ToInt32(dr["IDColor"].ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return image;
        }
    }
}
